<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/13
 * Time: 11:19
 */

namespace App\HttpController\V1;


class Reward extends Auth
{

    /**
     * 感召 激发 推荐代理   记录
     * @return bool
     */
    function reward(){
        $date=$this->input('date');// 2019-03 //如果为空表示查询全部
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $type=$this->input('type');
        if(!in_array($type,[1,2,3])){
            return $this->writeJson( -201, null, '参数错误' );
        }
        if($date){
            $dateStart=$date.'-01 00:00:00';
            $dateEnd=date('Y-m-d H:i:s',strtotime("{$dateStart} +1 month -1 second"));
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->where('coin_type','study')
                ->where('type',$type)
                ->where('class',1)
                ->whereBetween('createtime',[$dateStart,$dateEnd])
                ->orderBy('id')
                ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,source_id,money');
        }else{
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->where('coin_type','study')
                ->where('type',$type)
                ->where('class',1)
                ->orderBy('id')
                ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,source_id,money');
        }
        $money=$this->db
            ->where('user_id',$this->user->id)
            ->where('coin_type','study')
            ->where('type',$type)
            ->where('class',1)
            ->sum('users_account_log','money');
        $money=$money?:'0.00';
        foreach ($data as $k=>&$v){
            $users=$this->db->where('id',$v['source_id'])->getOne('users','nickname,headimg');
            $v['headimg']=QINIU_URL.$users['headimg'];
            $v['nickname']=$users['nickname'];
        }
        return $this->writeJson( 200, ['money'=>$money,'list'=>$data], '成功' );
    }

    /**
     * 收支明细
     */
    function budget(){
        $date=$this->input('date');// 2019-03 //如果为空表示查询全部
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $class=$this->input('class');
        if(!in_array($class,[1,2])){
            return $this->writeJson( -201, null, '参数错误' );
        }
        if($class==1){
            $fuhao='+';
        }else{
            $fuhao='-';
        }
        if($date){
            $dateStart=$date.'-01 00:00:00';
            $dateEnd=date('Y-m-d H:i:s',strtotime("{$dateStart} +1 month -1 second"));
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->where('coin_type','study')
                ->where('class',$class)
                ->whereBetween('createtime',[$dateStart,$dateEnd])
                ->orderBy('id')
                ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,type,source_id,money,remark');
        }else{
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->where('coin_type','study')
                ->where('class',$class)
                ->orderBy('id')
                ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,type,source_id,money,remark');
        }

        foreach ($data as $k=>&$v){
            $users=$this->db->where('id',$v['source_id'])->getOne('users','nickname,headimg');
            $v['nickname']=$users['nickname'];
            $v['money']=$fuhao.$v['money'];
            switch ($v['type']){
                case 1:
                    $v['type']='感召奖';
                    break;
                case 2:
                    $v['type']='激发奖';
                    break;
                case 3:
                    $v['type']='推荐代理奖';
                    break;
                case 4:
                    $v['type']='集体贡献';
                    break;
                case 5:
                    $v['type']='续费会员';
                    break;
                case 6:
                    $v['type']='商品购买';
                    break;
                case 7:
                    $v['type']='提现';
                    break;
                case 8:
                    $v['type']='驳回提现';
                    $v['nickname']='【系统】驳回提现';
                    break;
                case 9:
                    $v['type']='后台充值';
                    $v['nickname']='【系统】后台充值';
                    break;
                case 10:
                    $v['type']='活动报名';
                    break;
                default:
                    $v['type']=' ';
            }

            if($users['headimg']==null){
                $v['headimg']=QINIU_URL.'/img/user_head.png';
            }else{
                $v['headimg']=QINIU_URL.$users['headimg'];
            }

        }

        return $this->writeJson( 200, $data, '成功' );
    }

    /**
     * 爱心值
     */
    function love(){
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $class=$this->input('class');
        if(!in_array($class,[1,2])){
            return $this->writeJson( -201, null, '参数错误' );
        }
        if($class==1){
            $fuhao='+';
        }else{
            $fuhao='-';
        }
        $data=$this->db
            ->where('user_id',$this->user->id)
            ->where('coin_type','love')
            ->where('class',$class)
            ->orderBy('id')
            ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,money,remark');
        $money=$this->db->where('id',$this->user->id)->getValue('users','lover');

        foreach ($data as $k=>&$v){
            $v['money']=$fuhao.$v['money'];
        }
        return $this->writeJson( 200, ['money'=>$money,'list'=>$data], '成功' );
    }

    /**
     * 集体贡献
     */
    function collective(){
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $data=$this->db
            ->where('user_id',$this->user->id)
            ->where('type',4)
            ->orderBy('id')
            ->get('users_account_log',[($page-1)*$page_size,$page_size],'createtime,money,remark,source_id');
        foreach ($data as $k=>&$v){
            $users=$this->db->where('id',$v['source_id'])->getOne('users','nickname,headimg');
            $v['remark']=$users['nickname'];
        }
        $money=$this->db
            ->where('user_id',$this->user->id)
            ->where('type',4)
            ->sum('users_account_log','money');
        $money=$money?:'0.00';
        return $this->writeJson( 200, ['money'=>$money,'list'=>$data], '成功' );
    }





}